package com.shengqin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.entity.FreeTestApply;
import com.shengqin.mapper.FreeTestApplyMapper;
import com.shengqin.service.IFreeTestApplyService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class FreeTestApplyServiceImpl extends ServiceImpl<FreeTestApplyMapper, FreeTestApply> implements IFreeTestApplyService {
    @Override
    public Page<FreeTestApply> queryPage(CommonPage<FreeTestApply> freeTestApplyPage) {
        FreeTestApply queryParam = freeTestApplyPage.getQueryParam();
        QueryWrapper<FreeTestApply> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FreeTestApply::getPlanId, queryParam.getPlanId());
        wrapper.lambda().eq(FreeTestApply::getReservationId, queryParam.getReservationId());
        if (StringUtils.hasLength(queryParam.getName())) {
            wrapper.lambda().eq(FreeTestApply::getName, queryParam.getName());
        }
        if (StringUtils.hasLength(queryParam.getStudyCode())) {
            wrapper.lambda().eq(FreeTestApply::getStudyCode, queryParam.getStudyCode());
        }
        wrapper.lambda().orderByDesc(FreeTestApply::getId);
        Page<FreeTestApply> page = new Page<>(freeTestApplyPage.getPageNo(), freeTestApplyPage.getPageSize());
        return page(page, wrapper);
    }

    @Override
    public FreeTestApply getByStudyCodAndReservationId(String studyCode, Long reservationId) {
        QueryWrapper<FreeTestApply> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FreeTestApply::getStudyCode,studyCode);
        wrapper.lambda().eq(FreeTestApply::getReservationId, reservationId);
        return getOne(wrapper);
    }
}
